package com.itheima.controller;

import com.itheima.aspect.Log;
import com.itheima.pojo.Dept;
import com.itheima.service.DeptService;
import com.itheima.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/*部门*/
@RestController
@RequestMapping("/depts")
public class DeptController {
    @Autowired
    private DeptService deptService;

    /**
     * 删除部门
     *
     * @param id
     * @return
     */
    @Log
    @DeleteMapping
    public Result deleteById(Integer id) {
        deptService.deleteById(id);
        return Result.success();
    }

    /**
     * 查询全部部门
     *
     * @return
     */
    @GetMapping
    public Result selectDepts() {
        List<Dept> deptList = deptService.selectDepts();
        return Result.success(deptList);
    }

    /**
     * 添加部门
     *
     * @param dept
     * @return
     */
    @Log
    @PostMapping
    public Result addDept(@RequestBody Dept dept) {
        deptService.addDept(dept);
        return Result.success();
    }

    /**
     * 根据id查询部门
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        Dept dept = deptService.getById(id);
        return Result.success(dept);
    }

    /**
     * 修改部门
     *
     * @param dept
     * @return
     */
    @Log
    @PutMapping
    public Result updateById(@RequestBody Dept dept) {
        deptService.updateById(dept);
        return Result.success();
    }

}
